/*
 * .java		1.00	2010. 05. 01
 *
 * Copyright (c) 2010 doitframe team. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information
 * of doitframe team.  You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the license agreement
 * you entered into with doitframe team.
 */
package doit.common.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import doit.framework.util.PageUtils;

public class PagingHiddenTag extends TagSupport {

    /**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 8809617694116986270L;
	
	public static final String PGI_PAGE = "page";
	public static final String PGI_ORDER_BY = "orderBy";
	public static final String PGI_ROWS_PER_PAGE = "rowsPerPage";
	public static final String PGI_PAGES_PER_INDEX = "pagesPerIndex";
	
	private long   pagesPerIndex = 10;
	private long   page = 1;
	private long   rowsPerPage = 10;
	private String orderBy = "";

	public int doStartTag() throws JspException {
		try{
			HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
			
			if( request.getParameter(PGI_PAGES_PER_INDEX) != null && !request.getParameter(PGI_PAGES_PER_INDEX).equals(""))
				this.pagesPerIndex = Long.parseLong(request.getParameter(PGI_PAGES_PER_INDEX));
			if( request.getParameter(PGI_PAGE) != null && !request.getParameter(PGI_PAGE).equals(""))
				this.page = Long.parseLong(request.getParameter(PGI_PAGE));
			if( request.getParameter(PGI_ROWS_PER_PAGE) != null && !request.getParameter(PGI_ROWS_PER_PAGE).equals(""))
				this.rowsPerPage = Long.parseLong(request.getParameter(PGI_ROWS_PER_PAGE));
			if( request.getParameter(PGI_ORDER_BY) != null && !request.getParameter(PGI_ORDER_BY).equals(""))
				this.orderBy = request.getParameter(PGI_ORDER_BY);
		} catch(Exception e){
			throw new JspException("I/O 예외 : " + e.getMessage());
		}
		
        return SKIP_BODY;
    }

    public int doEndTag() throws JspException {
        try {
        	JspWriter out = pageContext.getOut();
        	
        	String html = PageUtils.generateInput(page, orderBy, rowsPerPage, pagesPerIndex);

			out.print(html);
        }
        catch (Exception e) {
            throw new JspTagException("I/O 예외 " + e.getMessage());
        }

        
        return EVAL_PAGE;
    }    

    public void release() {
        super.release();
        pagesPerIndex = 10;
    	page = 1;
    	rowsPerPage = 10;
    	orderBy = null;
    }
}
